<template>
  <div class="userManage">
    <el-row>
      <el-form :inline="true" :model="queryFrom" class="demo-form-inline">
        <el-form-item label="角色名称">
          <el-input v-model="queryFrom.roleName" placeholder="角色名称"></el-input>
        </el-form-item>
        <el-form-item label="角色编码">
          <el-input v-model="queryFrom.roleCode" placeholder="角色编码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="listByPage">查询</el-button>
          <el-button type="info" @click="refreshList">重置</el-button>
          <el-button type="text" @click="addUserWindow">新增</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">
        <el-table-column type="index" label="编号" width="180" />
        <el-table-column prop="roleName" label="角色名称" />
        <el-table-column prop="roleCode" label="角色编码" width="180" />
        <el-table-column label="权限管理" width="132" style="align-content: center">
          <template #default="scope">
            <el-button @click="openDialogVisiblePermission(scope)" type="warning" size="small">权限分配</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间" />
        <el-table-column fixed="right" label="操作" width="132" style="align-content: center">
          <template #default="scope">
            <el-button v-if="scope.row.roleCode !== 'admin'" @click="updateUserWindow(scope)" type="text" size="small">
              修改
            </el-button>
            <el-button v-if="scope.row.roleCode === 'admin'" type="text" size="small" disabled>-</el-button>
            <el-button v-if="scope.row.roleCode !== 'admin'" @click="deleteRow(scope)" type="text" size="small">删除</el-button>
            <el-button v-if="scope.row.roleCode === 'admin'" type="text" size="small" disabled>-</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryFrom.pageNo"
          :page-sizes="[5, 10, 20, 50, 100]"
          :page-size="queryFrom.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-row>
    <el-dialog v-model="dialogVisible" :title="'新增&修改'" width="580px">
      <el-form :model="addOrUpdateForm">
        <el-form-item label="角色名称" :label-width="100">
          <el-input v-model="addOrUpdateForm.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色编码" :label-width="100">
          <el-input v-model="addOrUpdateForm.roleCode" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addOrUpdateUser">确 定</el-button>
        </div>
      </template>
    </el-dialog>
    <!--  权限分配菜单  -->
    <el-dialog v-model="dialogVisiblePermission" :title="'权限分配菜单'" width="580px">
      <el-tree
        :data="permissionTableData"
        show-checkbox
        node-key="id"
        ref="tree"
        default-expand-all
        :default-expanded-keys="[2, 3]"
        :default-checked-keys="defaultExpandedKeys"
        :props="defaultProps"
      >
      </el-tree>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisiblePermission = false">取 消</el-button>
          <el-button type="primary" @click="permissionData">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { listByPage, deleteData, addData, updateData, permissionAllocation } from "@/api/sysManager/roleManage";
import { listTree } from "@/api/sysManager/menuManage/index.js";
export default {
  data() {
    return {
      permissionTableData: [],
      allocationVo: {
        menuId: [],
        roleId: ""
      },
      tableData: [],
      defaultExpandedKeys: [],
      dialogVisible: false,
      dialogVisiblePermission: false,
      passwordInput: false,
      addOrUpdateForm: {
        id: "",
        roleName: "",
        roleCode: ""
      },
      queryFrom: {
        pageNo: 1,
        pageSize: 10,
        roleId: "",
        roleName: "",
        roleCode: ""
      },
      total: 1,
      defaultProps: {
        children: "children",
        label: "name"
      }
    };
  },
  mounted() {
    this.listByPage();
  },
  methods: {
    // 获取分页查询数据
    // eslint-disable-next-line vue/no-dupe-keys
    async permissionData() {
      this.allocationVo.menuId = this.$refs.tree.getCheckedKeys();
      let res = await permissionAllocation(this.allocationVo);
      if (res.code === 200) {
        this.dialogVisiblePermission = false;
        this.$message.success(res.msg);
      }
    },
    // eslint-disable-next-line vue/no-dupe-keys
    async openDialogVisiblePermission(scope) {
      this.allocationVo.roleId = scope.row.id;
      this.queryFrom.roleId = scope.row.id;
      this.dialogVisiblePermission = true;
      let res = await listTree(this.queryFrom);
      if (res.code === 200) {
        this.permissionTableData = Array.of(res.data.menu);
        this.defaultExpandedKeys = res.data.menuId;
      }
    },
    // 获取分页查询数据
    async listByPage() {
      let res = await listByPage(this.queryFrom);
      if (res.code === 200) {
        this.tableData = res.data;
        this.total = res.total;
        if (res.total === 0) {
          this.$message.warning("当前条件未查询到数据");
        }
      }
    },
    async refreshList() {
      this.queryFrom.roleName = "";
      this.queryFrom.roleCode = "";
      this.listByPage();
    },
    addUserWindow() {
      this.addOrUpdateForm = {};
      this.passwordInput = true;
      this.dialogVisible = true;
    },
    updateUserWindow(scope) {
      this.dialogVisible = true;
      this.passwordInput = false;
      this.addOrUpdateForm = scope.row;
    },
    handleSizeChange(val) {
      this.queryFrom.pageSize = val;
      this.listByPage();
    },
    handleCurrentChange(val) {
      this.queryFrom.pageNo = val;
      this.listByPage();
    },
    async deleteRow(scope) {
      this.$confirm("此操作将永久删除, 是否继续?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          let res = await deleteData(scope.row.id);
          if (res.code === 200) {
            this.$message.success("删除成功");
            this.getList();
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async addOrUpdateUser() {
      let id = this.addOrUpdateForm.id;
      if (id != null || id != undefined) {
        this.updateData();
        return;
      }
      this.addData();
    },
    async updateData() {
      let res = await updateData(this.addOrUpdateForm);
      if (res.code === 200) {
        this.dialogVisible = false;
        this.listByPage();
        this.$message.success(res.msg);
      }
      return;
    },
    async addData() {
      let res = await addData(this.addOrUpdateForm);
      if (res.code === 200) {
        this.dialogVisible = false;
        this.listByPage();
        this.$message.success(res.msg);
      }
      return;
    },
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    tableRowClassName({ row, rowIndex }) {
      // eslint-disable-next-line @typescript-eslint/no-unused-vars
      let index = rowIndex % 2;
      if (index === 0) {
        return "warning-row";
      } else {
        return "success-row";
      }
    }
  }
};
</script>

<style>
.el-table .warning-row {
  background: #ffffff;
  /*background: #73ff00;*/
}

.el-table .success-row {
  background: #f0f9eb;
  /*background: #b1a59b;*/
}
</style>
